package cn.cms.chuangjia.controller;

import cn.cms.chuangjia.domain.Admin;
import cn.cms.chuangjia.service.IAdminService;
import cn.cms.chuangjia.vo.AjaxResult;
import cn.cms.chuangjia.vo.Login;
import cn.cms.chuangjia.vo.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * adminController
 *
 * @author ruoyi
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/cms/admin")
@Slf4j
@Api("和管理员相关操作")
public class AdminController extends BaseController {
    @Autowired
    private IAdminService adminService;

    /**
     * 查询admin列表
     */
    //@PreAuthorize("@ss.hasPermi('cms:admin:list')")
    @ApiOperation("管理员分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页参数",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "页大小",dataType = "int",paramType = "query"),
    })
    @GetMapping("/list")
    public Page list(Admin admin) {
        log.debug("分页开始!!!!!!!!!!");
        startPage();
        List<Admin> list = adminService.selectAdminList(admin);
        PageInfo<Admin> page = PageInfo.of(list);

        return Page.convert(page);
    }

    /**
     * 导出admin列表
     */
    //@PreAuthorize("@ss.hasPermi('cms:admin:export')")
    //@Log(title = "admin", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Admin admin)
//    {
//        List<Admin> list = adminService.selectAdminList(admin);
//        ExcelUtil<Admin> util = new ExcelUtil<Admin>(Admin.class);
//        util.exportExcel(response, list, "admin数据");
//    }

    /**
     * 获取admin详细信息
     */
    //@PreAuthorize("@ss.hasPermi('cms:admin:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(adminService.selectAdminById(id));
    }

    /**
     * 新增admin
     */
    //@PreAuthorize("@ss.hasPermi('cms:admin:add')")
    //@Log(title = "admin", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Admin admin) {
        return AjaxResult.toAjax(adminService.insertAdmin(admin));
    }

    /**
     * 修改admin
     */
    //@PreAuthorize("@ss.hasPermi('cms:admin:edit')")
    //@Log(title = "admin", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Admin admin) {
        return AjaxResult.toAjax(adminService.updateAdmin(admin));
    }

    /**
     * 删除admin
     */
    //@PreAuthorize("@ss.hasPermi('cms:admin:remove')")
    //@Log(title = "admin", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return AjaxResult.toAjax(adminService.deleteAdminByIds(ids));
    }

    //登录
    @PostMapping("/login")//@RequestBody
    public AjaxResult login(@RequestBody Login admin, HttpServletRequest request) {
        String code = admin.getVcode();
        String remember = admin.getRemeberMe();
        String sessionCode = (String) request.getSession().getAttribute("kaptcha");
        System.out.println("in sesson========" + sessionCode + "....." + code);
        if (!Objects.equals(code, sessionCode)) {
            return AjaxResult.error(401, "验证码错误");
        }
        Admin temp = new Admin();
        temp.setLoginname(admin.getLoginname());
        temp.setPass(admin.getPass());
        Admin ad = adminService.login(temp);
        if (ad == null) {
            return AjaxResult.error(403, "错误的用户名或者密码");
        }
        if (ad.getIsvalid() == false) {
            return AjaxResult.error(404, "该用户已被禁用");
        }
        //session...
        request.getSession().setAttribute("Admin", ad);
        return AjaxResult.success(ad);
    }

    @GetMapping("logout")
    public AjaxResult login(HttpSession session) {
        session.removeAttribute("Admin");
        return AjaxResult.success();
    }
}
